/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

// 多选开关
window.DataTableMuli = false;

define(['jquery', 'Spinner'], function($, Spinner) {

    var Util = {};

    /**
     * loading animate
     * @type @exp;Object@call;onew
     */
    window.Loading = Object.onew({
        start: function(id) {
            Spinner.spin($(id).get(0));
        },
        finish: function() {
            Spinner.spin().stop();
        }
    });

    window.wpLoading = function(id) {
        id = id || "#main-mid";
        Loading.start(id);
    };

    /**
     * 
     * @param {type} message
     * @param {type} warn
     * @returns {undefined}
     */
    Util.alert = function(message, warn) {
        warn = warn || false;
        if (warn) {
            $('#__alert__').addClass('warn');
        } else {
            $('#__alert__').removeClass('warn');
        }
        $('#__alert__').slideDown().html(message);
        window.setTimeout(function() {
            $('#__alert__').slideUp();
        }, 1500);
    };

    /**
     * datatable事件监听
     * @returns {undefined}
     */
    Util.dataTableLis = function(tableId, sel) {
        tableId = tableId || '.dTable';
        // 是否可以选中 默认true
        sel = sel || true;
        $(tableId + ' tbody tr').unbind('click').click(function() {
            if (sel) {
                if (!DataTableMuli) {
                    // 单选
                    $(tableId + ' tbody tr.click').removeClass('click');
                    $(this).addClass('click');
                    dataTableSelectedTr = $(this);
                } else {
                    // 多选
                    var cb = $(this).find('input:checkbox')[0];
                    if ($(this).hasClass('click')) {
                        if (cb) {
                            cb.checked = false;
                        }
                        $(this).toggleClass('click');
                    } else {
                        $(this).addClass('click');
                        if (cb) {
                            cb.checked = true;
                        }
                    }
                }
            }
            $('.button.del,.button.edit').css('display', 'inline-block');
        }).mouseover(function() {
            $(this).addClass('hover');
        }).mouseout(function() {
            $(this).removeClass('hover');
        });
    };

    window.dataTableLis = Util.dataTableLis;

    Util.resize = function() {
        console.log(1);
    };

    Util.Alert = function(message, warn) {
        warn = warn || false;
        var node = $('<div id="__alert__"></div>');
        $('body').append(node);
        if (warn) {
            node.addClass('warn');
        } else {
            node.removeClass('warn');
        }
        node.slideDown().html(message);
        window.setTimeout(function() {
            node.fadeOut(1500);
            window.setTimeout(function() {
                $('#__alert__').remove();
            }, 1500);
        }, 2000);
    };

    return Util;
});

Object.onew = function(o) {
    var F = function(o) {
    };
    F.prototype = o;
    return new F;
};